package com.example.service;

import java.util.List;

import com.example.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.pojo.User;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public List<User> listAllUser() {
        return userMapper.findAllUser();
    }

    public boolean saveUser(User user) {
        final User existsUser = userMapper.findOne(user.getId());
        if (existsUser != null) {
            return false;
        } else {
            userMapper.saveUser(user);
            return true;
        }
    }

    public boolean deleteUser(Long id) {
        return userMapper.deleteUser(id) == 1;
    }

    public boolean updateUser(User user) {
        final User existsUser = userMapper.findOne(user.getId());
        if (existsUser != null) {
            return userMapper.updateUser(user) == 1;
        }
        return false;
    }

    public User getUser(Long id) {
        return userMapper.findOne(id);
    }
}
